<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/03/0003
 * Time: 10:54
 */

namespace app\sports_admin\service;

use app\exception\ContentsException;
use app\exception\RegionException;
use think\Request;
use app\sports_admin\model\Contents as ContentsModel;
use app\sports_admin\model\Region as RegionModel;
class Contents
{
    /**
     * 添加内容
     * @param $project_id 项目id
     * @param $city 市区名称
     * @param $high_score 最高分
     * @param $low_score 最低分
     * @param $sex 性别
     * @param $score 分数
     */
    public static function insert()
    {

        $params = Request::instance()->only(['project_id','city','high_score','low_score','sex','score']);
        $params['region_id'] = RegionModel::getCityId($params['city']);
        if(!$params['region_id']){
            throw new RegionException([
                'msg'=>'请添加正确的城市名称',
                'errorCode'=>404
            ]);
        }
        $params['year'] = date('Y',time());
        unset($params['city']);
        $contentsId = ContentsModel::contentsInsert($params);
        if(!$contentsId){
            throw new ContentsException([
                'msg'=>'内容信息添加失败',
                'errorCode'=>400,
            ]);
        }
        return json(['code'=>200,'msg'=>'添加成功']);
    }
    /**
     * 添加内容
     * @param $id 内容
     * @param $project_id 项目id
     * @param $city 市区名称
     * @param $high_score 最高分
     * @param $low_score 最低分
     * @param $sex 性别
     * @param $score 分数
     */
    public static function update()
    {

        $params = Request::instance()->only(['id','project_id','city','high_score','low_score','sex','score']);
        $id = $params['id'];
        $params['region_id'] = RegionModel::getCityId($params['city']);
        if(!$params['region_id']){
            throw new RegionException([
                'msg'=>'请添加正确的城市名称',
                'errorCode'=>404
            ]);
        }
        $params['year'] = date('Y',time());
        unset($params['city']);
        unset($params['id']);
        $contentsId = ContentsModel::contentsUpdate($id,$params);
        if(!$contentsId){
            throw new ContentsException([
                'msg'=>'内容信息修改失败',
                'errorCode'=>400,
            ]);
        }
        return json(['code'=>200,'msg'=>'修改成功']);
    }


}